package com.situ.ssm.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.ssm.dao.StudentMapper;
import com.situ.ssm.entity.Banji;
import com.situ.ssm.entity.PageBean;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;
import com.situ.ssm.vo.StudentSearchCondition;

@Service
public class StudentServiceImpl implements IStudentService {
	//自动注入
	@Autowired
	private StudentMapper studentDao;
	
	@Override
	public List<Student> list() {
		return studentDao.list();
	}
	//分页
	@Override
	public PageBean<Map<String, Object>> getPageBean(StudentSearchCondition condition) {
		PageBean<Map<String, Object>> pageBean = new PageBean<>();
		pageBean.setPageNo(condition.getPageNo());
		pageBean.setPageSize(condition.getPageSize());
		//总记录数（是符合condition条件的总记录数）
		Integer totalCount = studentDao.getTotalCount(condition);
		pageBean.setTotalCount(totalCount);
		//一共有多少页
		Integer totalPage = (int)Math.ceil((double)totalCount / condition.getPageSize());
		pageBean.setTotalPage(totalPage);
		//得到当前页的数据
		Integer offset = (condition.getPageNo() - 1) * condition.getPageSize();
		List<Map<String, Object>> list = studentDao.findPageBeanList(condition, offset);
		pageBean.setList(list);
		return pageBean;
	}
	//添加
	@Override
	public boolean insert(Student student) {
		Integer count = studentDao.insert(student);
		return count == 1 ? true : false ;
	}
	//Id删除
	@Override
	public boolean deleteById(Integer id) {
		Integer count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}
	//批量删除
	@Override
	public boolean deleteAll(String[] selectIds) {
		Integer count = studentDao.deleteAll(selectIds);
		return count > 0 ? true : false;
	}
	//添加前展示
	@Override
	public Student findById(Integer id) {
		return studentDao.findById(id);
	}
	//添加
	@Override
	public boolean update(Student student) {
		Integer count  = studentDao.update(student);
		return count == 1 ? true : false;
	}
	//ajax列表不重复
	@Override
	public boolean findByName(String name) {
		Integer count =  studentDao.findByName(name);
		return count > 0 ? true : false;
	}

}
